import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:payment/model/PayAndBal.dart';
import 'package:payment/util/DBHelper.dart';
import 'package:payment/util/MoneyUtil.dart';

class PayListView extends StatefulWidget {
  final List<PayAndBal> _payAndBals;
  Function getPayAndBal;
  PayListView(this._payAndBals, this.getPayAndBal);

  @override
  createState() => new PayListViewState();
}

class PayListViewState extends State<PayListView> {
  DBHelper dbHelper = new DBHelper();
  final dateFormat = DateFormat('yyyy年MM月dd日 hh:mm');
  final monthFormat=DateFormat('M月d日 hh:mm');

  MoneyUtil moneyUtil = new MoneyUtil();
  @override
  Widget build(BuildContext context) {
    return _buildPayAndBalList();
  }

  Widget _buildPayAndBalList() {
    return widget._payAndBals.length != 0
        ? Expanded(
            child: ListView.separated(
              padding: const EdgeInsets.symmetric(vertical: 5),
              itemBuilder: (context, i) {
                final item = widget._payAndBals[i];
                return Dismissible(
                    key: Key(item.id.toString()),
                    onDismissed: (direction) {
                      setState(() {
                        dbHelper.deletePayAndBal(item.id);
                        widget._payAndBals.removeAt(i);
                        widget.getPayAndBal();
                      });
                      ScaffoldMessenger.of(context)
                          .showSnackBar(SnackBar(content: Text('删除成功')));
                    },
                    child: _buildRow(item));
              },
              separatorBuilder: (BuildContext context, int index) =>
                  const Divider(),
              itemCount: widget._payAndBals.length,
            ),
          )
        : Padding(
            padding: EdgeInsets.symmetric(vertical: 10),
            child: Text("无收支记录数据"));
  }

  Widget _buildRow(PayAndBal payAndBal) {
    var symbol = payAndBal.isPay ? '' : '+';
    String payAndBalType = payAndBal.isPay ? '支出' : '收入';
    Color color = !payAndBal.isPay ? Colors.deepOrange : Colors.black;
    return new ListTile(
        leading: Image.asset(
          'assets/' + payAndBal.imageStr + '.png',
          height: 30,
          width: 30,
        ),
        contentPadding: EdgeInsets.all(0),
        title: Transform.translate(
          offset: Offset(-16, 0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                payAndBal.consumptionObject,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
              ),
              Padding(
                padding: EdgeInsets.symmetric(vertical: 1),
                child: Text(
                  payAndBalType +
                      '-' +
                      payAndBal.classification +
                      '  储蓄卡' +
                      payAndBal.card.toString(),
                  style: TextStyle(fontSize: 12, color: Colors.grey),
                ),
              ),
              Text(
                monthFormat.format(dateFormat.parse(payAndBal.updateTime)),
                style: TextStyle(fontSize: 12, color: Colors.grey),
              )
            ],
          ),
        ),
        trailing: Column(
          children: [
            Text(
              symbol + payAndBal.consumptionNum.toStringAsFixed(2) + '元',
              style: TextStyle(
                  fontSize: 20, fontWeight: FontWeight.bold, color: color),
            ),
            Text(
              "余额" +
                  moneyUtil.moneyFormat(payAndBal.total.toStringAsFixed(2)) +
                  '元',
              style: TextStyle(fontSize: 12, color: Colors.grey),
            )
          ],
        ));
  }
}
